



const Cartesian3 = Cesium.Cartesian3;
const ComponentDatatype = Cesium.ComponentDatatype;
const PrimitiveType = Cesium.PrimitiveType;
const BoundingSphere = Cesium.BoundingSphere;
const GeometryAttribute = Cesium.GeometryAttribute;
const GeometryAttributes = Cesium.GeometryAttributes;
const GeometryPipeline = Cesium.GeometryPipeline;
const VertexFormat = Cesium.VertexFormat;
const Geometry = Cesium.Geometry;
const Matrix3 = Cesium.Matrix3;

var sin = Math.sin;
var cos = Math.cos;
var tan = Math.tan;
var atan = Math.atan;
var asin = Math.asin;

// 缓存转换变量
var matrix3Scratch = new Matrix3();

var fineness = [60, 30, 15, 10, 5, 3, 2, 1];


var deflection = (matrix, Point) => Matrix3.multiplyByVector(matrix, Point, new Cesium.Cartesian3())



var Sector = function (slice, xStart, xEnd, yStart, yEnd) {
    var _f = fineness[slice - 1];
    var xLength = Math.ceil((xEnd - xStart) / _f) + 1;
    var yLength = Math.ceil((yEnd - yStart) / _f) + 1;


 
    //zox面单位圆
    var zox = [];
    // for (var i = 0; i < xLength; i++) {
    //     var phi = 2 * maxX * i / (xLength - 1) - maxX;

    //     console.log(phi);

    //     zox.push(new Cartesian3(0, sin(phi), cos(phi)));
    // }
    for (let i = 0; i < yLength; i++) {
        var _Angle = i * _f + yStart;
        if (_Angle > yEnd) _Angle = yEnd;
        _Angle += 90
        zox.push(new Cartesian3(0, cos(_Angle * (Math.PI / 180)), sin(_Angle * (Math.PI / 180))));
    }
    return zox;
}



function computeUnitPosiiton(slice, xStart, xEnd, yStart, yEnd) {
    var zo = Sector(slice, xStart, xEnd, yStart, yEnd);
    // const positions = new Float64Array(0);
    // const indices = new Uint16Array(0);

    console.log(zo);

    var positions = new Float64Array((zo.length + 1) * 3);
    positions[0] = 0.0;
    positions[1] = 0.0;
    positions[2] = 0.0;

    var LeftIndex = 3
    for (let i = 0; i < zo.length; i++) {
        const element = zo[i];
        positions[LeftIndex++] = element.x;
        positions[LeftIndex++] = element.y;
        positions[LeftIndex++] = element.z;
    }
    const indices = new Uint16Array(positions.length);

    var LeftIndex_ = 0;
    for (let i = 0; i < zo.length - 1; i++) {
        indices[LeftIndex_++] = 0;
        indices[LeftIndex_++] = i + 1;
        indices[LeftIndex_++] = i + 2;
    }

    return { positions, indices }
}


const radarGeometry = function (xStart, xEnd, yStart, yEnd) {
    var _Obj = computeUnitPosiiton(3, xStart, xEnd, yStart, yEnd);

    console.log(_Obj);

    // 创建顶点属性中的坐标
    const attributes = new GeometryAttributes({
        position: new GeometryAttribute({
            componentDatatype: ComponentDatatype.DOUBLE,
            componentsPerAttribute: 3,
            values: _Obj.positions
        })
    });

    // 指定此四面体的各种属性
    this.attributes = attributes;
    this.indices = _Obj.indices;
    this.primitiveType = PrimitiveType.TRIANGLES;
    this.boundingSphere = new BoundingSphere(new Cartesian3(0.0, 0.0, 0.0), 1.0);
}

// radarGeometry(0, 180, 0, 180);

export default radarGeometry